package com.zb.set.service.impl;

import com.zb.entity.AccounLoginInfo;
import com.zb.exception.SMSException;
import com.zb.service.*;
import com.zb.set.entity.SystemInitVo;
import com.zb.set.service.SetService;
import com.zb.util.PCSmsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author WEN
 * @Date 2023/7/26 0026
 **/
@Service
public class SetServiceImpl implements SetService {
    @Resource
    private ZjEmpService empService;
    @Resource
    private ZjCodeRegSetService codeRegSetService;
    @Resource
    private ZjCodeSetService codeSetService;
    @Resource
    private ZjCodeScanDetailSetService codeScanDetailSetService;
    @Resource
    private ZjNetworkSetService networkSetService;
    @Resource
    private ZjRepairSetService repairSetService;
    @Resource
    private ZjStoreSetService storeSetService;
    @Resource
    private ZjSysSetOverallService sysSetOverallService;
    @Resource
    private ZjUserSetService userSetService;
    @Resource
    private ZjWorkSetService workSetService;
    @Resource
    private ZjFactorySetService factorySetService;

    // 初始化系统所有设置
    @Override
    @Transactional
    public void initSystemSet(SystemInitVo systemInitVo) throws SMSException {
        checkInitSystemCode(systemInitVo.getCustAccId(), systemInitVo.getCode());

        for (Integer initDataType : systemInitVo.getType()) {
            switch (initDataType) {
                case 1: // 系统全局设置
                    sysSetOverallService.initOrRestoreDefaultSysSetOverall(systemInitVo.getCustAccId());
                    break;

                case 2: // 编码设置
                    codeSetService.initOrRestoreDefaultCodeSet(systemInitVo.getCustAccId());
                    break;

                case 3: // 编码扫码详情设置
                    codeScanDetailSetService.initOrRestoreDefaultCodeScanDetailSet(systemInitVo.getCustAccId());
                    break;

                case 4: // 编码登记设置
                    codeRegSetService.initOrRestoreDefaultCodeRegSet(systemInitVo.getCustAccId());
                    break;

                case 5: // 服务区域设置
                    networkSetService.initOrRestoreDefaulNetworkSet(systemInitVo.getCustAccId());
                    break;

                case 6: // 用户设置
                    userSetService.initOrRestoreDefaultUserSet(systemInitVo.getCustAccId());
                    break;

                case 7: // 工单设置
                    workSetService.initOrRestoreDefaultWorkSet(systemInitVo.getCustAccId());
                    break;

                case 8: // 返厂设置
                    repairSetService.initOrRestoreDefaultRepairSet(systemInitVo.getCustAccId());
                    break;

                case 9: // 仓库设置
                    storeSetService.initOrRestoreDefaultStoreSet(systemInitVo.getCustAccId());
                    break;
                case 10: // 品牌厂商设置
                    factorySetService.initOrRestoreDefaultFactorySet(systemInitVo.getCustAccId());
                default:
                    throw new IllegalArgumentException("初始化数据类型参数不合法！");
            }
        }
    }

    // 初始化编码登记设置
    @Override
    public void initOrRestoreDefaultCodeRegSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        codeRegSetService.initOrRestoreDefaultCodeRegSet(custAccId);
    }

    // 初始化编码设置
    @Override
    public void initOrRestoreDefaultCodeSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        codeSetService.initOrRestoreDefaultCodeSet(custAccId);
        codeScanDetailSetService.initOrRestoreDefaultCodeScanDetailSet(custAccId);
    }

    // 初始化网点设置
    @Override
    public void initOrRestoreDefaulNetworkSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        networkSetService.initOrRestoreDefaulNetworkSet(custAccId);
    }

    // 初始化返厂设置
    @Override
    public void initOrRestoreDefaultRepairSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        repairSetService.initOrRestoreDefaultRepairSet(custAccId);
    }

    // 初始化仓储设置
    @Override
    public void initOrRestoreDefaultStoreSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        storeSetService.initOrRestoreDefaultStoreSet(custAccId);
    }

    // 初始化系统全局设置
    @Override
    public void initOrRestoreDefaultSysSetOverall(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        sysSetOverallService.initOrRestoreDefaultSysSetOverall(custAccId);
    }

    // 初始化用户设置
    @Override
    public void initOrRestoreDefaultUserSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        userSetService.initOrRestoreDefaultUserSet(custAccId);
    }

    // 初始化工单设置
    @Override
    public void initOrRestoreDefaultWorkSet(AccounLoginInfo accountLoginInfo, String code) throws SMSException {
        Long custAccId = accountLoginInfo.getCustAccId();
        checkInitSystemCode(custAccId, code);
        workSetService.initOrRestoreDefaultWorkSet(custAccId);
    }

    /**
     * 校验初始化系统编码
     * @param custAccId 商家ID
     * @param code 验证码
     * @throws SMSException
     */
    private void checkInitSystemCode(Long custAccId, String code) throws SMSException {
        // 获取系统超管手机号
        String phone = empService.getSuperAccountByCustAccId(custAccId);
        PCSmsUtil.checkInitSystemCode(phone, code);
    }
}
